package com.example.service.impl;

import com.example.common.CaptureConfig;
import com.example.common.CustomException;
import com.example.dao.MemberDao;
import com.example.entity.Member;
import com.example.service.MemberService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * 会员服务实现类
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberDao memberDao;

    @Override
    public Member login(Member member, HttpServletRequest request, HashMap<String, Object> map) {
        map.put("username", member.getUsername());
        map.put("password", member.getPassword());
        String key = request.getParameter("key");
        
        // 判断验证码
        if (!member.getVerCode().toLowerCase().equals(CaptureConfig.CAPTURE_MAP.get(key))) {
            throw new CustomException("验证码错误");
        }
        
        List<Member> list = memberDao.selectOne(map);
        if (list.size() == 0) {
            throw new CustomException("用户名或密码错误或账号已冻结");
        } else {
            request.getSession().setAttribute("member", list.get(0));
        }
        
        // 登录成功后清除正确的验证码
        CaptureConfig.CAPTURE_MAP.remove(key);
        
        // 获取当前登录用户信息
        return (Member) request.getSession().getAttribute("member");
    }

    @Override
    public void register(Member member, HashMap<String, Object> map) {
        map.put("username", member.getUsername());
        List<Member> list = memberDao.selectAll(map);
        
        // 判断用户名的唯一性
        if (list.size() > 0) {
            throw new CustomException("已存在的用户名，请更换");
        }
        
        memberDao.add(member);
    }

    @Override
    public PageInfo<Member> getMemberList(Integer pageNum, Integer pageSize, String key, String key1) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("key", key);
        map.put("key1", key1);
        
        PageHelper.startPage(pageNum, pageSize);
        List<Member> list = memberDao.selectAll(map);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Member member, HttpServletRequest request) {
        if (member.getId() == null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("username", member.getUsername());
            List<Member> list = memberDao.selectAll(map);
            
            // 判断用户名的唯一性
            if (list.size() > 0) {
                throw new CustomException("该用户名以存在，请更换");
            }
            
            memberDao.add(member);
        } else {
            memberDao.update(member);
        }
    }

    @Override
    public Member updateMemberStatus(Member member) {
        memberDao.update(member);
        return memberDao.findById(member.getId());
    }

    @Override
    public void updatePassword(Member member, String onlpassword, String confirmPasword) {
        if (!onlpassword.equals(member.getPassword())) {
            throw new CustomException("原密码错误");
        }
        
        member.setPassword(confirmPasword);
        memberDao.update(member);
    }

    @Override
    public void deleteMember(int id) {
        memberDao.delete(id);
    }

    @Override
    public void batchDeleteMember(String vals) {
        String[] val = vals.split(",");
        for (String id : val) {
            memberDao.delete(Integer.parseInt(id));
        }
    }
} 